package oa.hleast.xswl.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hleast.sms.pojo.domain.CmsVenueSchedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.common.constant.ReviewStatusEnum;
import oa.hleast.xswl.pojo.domain.CmsVenueMain;
import oa.hleast.xswl.pojo.domain.CmsVenueSignIn;
import oa.hleast.xswl.pojo.form.VenueScheduleAddForm;
import oa.hleast.xswl.pojo.vo.CmsVenueScheduleVo;
import oa.hleast.xswl.pojo.vo.CmsVenueSignInVo;
import oa.hleast.xswl.pojo.vo.VenueUserSignInVo;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.venue.CmsVenueMainService;
import oa.hleast.xswl.service.venue.CmsVenueScheduleService;
import oa.hleast.xswl.service.venue.CmsVenueSignInService;
import oa.hleast.xswl.service.venue.VenueService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "【系统管理】场馆预约")
@RestController("adminVenueController")
@RequestMapping("/api.admin/v1/venue")
@Slf4j
@AllArgsConstructor
public class VenueController {

    private final CmsVenueMainService cmsVenueMainService;
    private final CmsVenueScheduleService cmsVenueScheduleService;
    private final CmsVenueSignInService cmsVenueSignInService;
    private final VenueService venueService;
    private final IUmsUserService umsUserService;


    @ApiOperation(value = "场馆列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "departmentId", value = "所属部门ID 0图书馆 1文化馆", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/main/list")
    public Result mainList(
            String queryMode,
            Integer page,
            Integer limit,
            Long departmentId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                LambdaQueryWrapper<CmsVenueMain> queryWrapper = new LambdaQueryWrapper<CmsVenueMain>()
                        .orderByDesc(CmsVenueMain::getGmtModified)
                        .orderByDesc(CmsVenueMain::getGmtCreate);

                Page<CmsVenueMain> result = cmsVenueMainService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }


    @ApiOperation(value = "新增场馆", httpMethod = "POST")
    @PostMapping("/main/add")
    public Result add(@RequestBody CmsVenueMain body) {
        boolean status = cmsVenueMainService.save(body);
        return Result.judge(status);
    }

    @ApiOperation(value = "场馆详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/mainDetail/{id}")
    public Result detail(@PathVariable Long id) {

        return Result.success(cmsVenueMainService.getById(id));
    }

    @ApiOperation(value = "修改场馆", httpMethod = "PUT")
    @PutMapping("/main/update")
    public Result update(
            @RequestBody CmsVenueMain body) {
        body.setGmtModified(new Date());
        boolean status = cmsVenueMainService.updateById(body);
        return Result.judge(status);
    }

    @ApiOperation(value = "删除场馆", httpMethod = "GET")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @GetMapping("/mainDelete/{ids}")    // TODO 这个是用GetMpping 还是使用DeleteMapping
    public Result delete(@PathVariable("ids") String ids) {
        boolean status = cmsVenueMainService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }


    @ApiOperation(value = "场次列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "venueMainId", value = "场馆id", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/schedule/list")
    public Result list(
            String queryMode,
            Integer page,
            Integer limit,
            Long venueMainId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                LambdaQueryWrapper<CmsVenueSchedule> queryWrapper = new LambdaQueryWrapper<CmsVenueSchedule>()
                        .eq(CmsVenueSchedule::getVenueMainId, venueMainId)
                        .orderByDesc(CmsVenueSchedule::getGmtModified)
                        .orderByDesc(CmsVenueSchedule::getGmtCreate);

                Page<CmsVenueSchedule> result = cmsVenueScheduleService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    /**
     * 按年月查找场馆的场次
     */
    @ApiOperation(value = "按年月查找场馆的场次", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/schedule/findScheduleByYearAndMonth")
    public Result findScheduleByYearAndMonth(@RequestParam Integer year, @RequestParam Integer month, @RequestParam Long venueMainId) {
        List<CmsVenueScheduleVo> venueScheduleList = venueService.findScheduleByYearAndMonth(year, month, venueMainId);
        return Result.success(venueScheduleList);
    }


    @ApiOperation(value = "批量新增场次", httpMethod = "POST")
    @PostMapping("/schedule/addList")
    public Result scheduleAddList(@RequestBody VenueScheduleAddForm form) {
        boolean status = venueService.saveScheduleList(form);
        return Result.judge(status);
    }

    @ApiOperation(value = "新增场次", httpMethod = "POST")
    @PostMapping("/schedule/add")
    public Result scheduleAdd(@RequestBody VenueScheduleAddForm form) {
        boolean status = venueService.saveSchedule(form);
        return Result.judge(status);
    }

    @ApiOperation(value = "更新场次", httpMethod = "GET")
    @GetMapping("/schedule/update")
    public Result scheduleAdd(@RequestParam Long id, @RequestParam Integer amOpen, @RequestParam Integer pmOpen) {
        boolean update = cmsVenueScheduleService.update(new LambdaUpdateWrapper<CmsVenueSchedule>()
                .set(CmsVenueSchedule::getAmOpen, amOpen)
                .set(CmsVenueSchedule::getPmOpen, pmOpen)
                .eq(CmsVenueSchedule::getId, id));
        return Result.judge(update);
    }

    @ApiOperation(value = "场次详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/scheduleDetail/{id}")
    public Result scheduleDetail(@PathVariable Long id) {
        return Result.success(cmsVenueScheduleService.getById(id));
    }

    @ApiOperation(value = "删除场次", httpMethod = "GET")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @GetMapping("/scheduleDelete/{ids}")
    public Result scheduleDelete(@PathVariable("ids") String ids) {
        boolean status = cmsVenueScheduleService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

    @ApiOperation(value = "报名情况列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "venuesMainId", value = "场馆id", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/signIn/list")
    public Result signList(
            String queryMode,
            Integer page,
            Integer limit,
            Long venuesMainId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<VenueUserSignInVo> pageVo = venueService.pageUserSign(page, limit, venuesMainId);

                return Result.success(pageVo.getRecords(), pageVo.getTotal());
        }
    }

    @ApiOperation(value = "详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/signIn/{id}")
    public Result signInDetail(@PathVariable Long id) {

        CmsVenueSignInVo vo = venueService.signInDetail(id);
        return Result.success(vo);
    }

    @ApiOperation(value = "审核通过", httpMethod = "GET")
    @GetMapping("/signIn/pass")
    public Result review(@RequestParam Long signInId) {

        boolean status = cmsVenueSignInService.update(new LambdaUpdateWrapper<CmsVenueSignIn>()
                .eq(CmsVenueSignIn::getId, signInId)
                .set(CmsVenueSignIn::getReviewStatus, ReviewStatusEnum.PASS.getCode()));

        return Result.judge(status);
    }

    @ApiOperation(value = "审核驳回", httpMethod = "GET")
    @GetMapping("/signIn/notPass")
    public Result review(@RequestParam Long signInId, @RequestParam String remark) {

        boolean status = cmsVenueSignInService.update(new LambdaUpdateWrapper<CmsVenueSignIn>()
                .eq(CmsVenueSignIn::getId, signInId)
                .set(CmsVenueSignIn::getReviewStatus, ReviewStatusEnum.NO_PASS.getCode())
                .set(CmsVenueSignIn::getReviewRemark, remark));

        return Result.judge(status);
    }

}
